package com.own.business.question.business.service.impl;

import com.own.business.question.business.service.PracticeHistoryService;
import com.own.business.question.business.service.QuestionService;
import com.own.business.question.entity.bo.PracticeHistoryBo;
import com.own.business.question.entity.map.PracticeHistoryMapVo;
import com.own.business.question.entity.po.Practice;
import com.own.business.question.entity.po.PracticeHistory;
import com.own.business.question.entity.query.PracticeHistoryQuery;
import com.own.business.question.entity.vo.PracticeHistoryVo;
import com.own.business.question.mapper.PracticeHistoryMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.exception.BusinessException;
import com.own.component.common.model.UpdateModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * PracticeHistoryServiceImpl
 *
 * @author 陈学礼 （你是一段成熟的代码，要学会自己改bug）
 * @version 4.0.0
 * @date 2021/05/14 15:49:28
 * @since 4.0.0
 */
@Slf4j
@Service
public class PracticeHistoryServiceImpl extends AbstractBaseService<
        PracticeHistory,
        PracticeHistoryBo,
        PracticeHistoryVo,
        PracticeHistoryMapVo,
        PracticeHistoryQuery,
        PracticeHistoryMapper
        > implements PracticeHistoryService {

    @Resource
    private QuestionService questionService;

    /**
     * 提交答案
     *
     * @param pk      流水id
     * @param answer  答案信息
     * @param useTime 用时
     * @return 是否提交成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<Boolean> submit(Long pk, String answer, Long useTime) {
        var po = getPoById(pk);
        switch (po.getStatus()) {
            case Practice.Status.NO_ANSWER:
            case Practice.Status.SAVE:
                break;
            case Practice.Status.ANSWER:
                throw new BusinessException("当前练习已经确认过了，无法重新提交答案信息");
            case Practice.Status.ABANDON:
                throw new BusinessException("当前练习已经被废弃，无法提交答案信息");
            default:
                throw new BusinessException("练习的状态错误：{}", pk + "");
        }
        po.setStatus(Practice.Status.SAVE);
//        var question = questionService.getPoById( po.getFkQuestionId());
//        // 确定答案的信息，因为是打乱的数据
//        if (question.getType().equals(Question.Type.SINGLE_CHOICE) || question.getType().equals(Question.Type.MULTIPLE_CHOICE)) {
//
//            po.getKeySortOrder();
//
//
//        } else {
//
//        }
        po.setAnswer(answer);
        po.setUseTime(po.getUseTime() == null ? useTime : (po.getUseTime() + useTime));
        return new UpdateModel<>(update(po).getFlag());
    }


}
